/**
 * Created by huangzy on 2015/5/22.
 * //wx.12301.cc/tpl/membercard/js/membercard.renzheng.js
 */

var ImgUploador = ZFT.Util.Class({
    statics: {
        ltie9: false
    },
    EVENTS: {
        "change input.fileInp": "onFileInpChange"
    },
    init: function (opt) {
        var that = this;
        this.statics.ltie9 = $("#memCertifyContainer").hasClass("lt-ie9") ? true : false;
        var container = this.container = opt.container;
        var containerId = container.attr("id");
        var form = this.form = container.find("form");
        var label = this.label = container.find("label");
        var fileInp = this.fileInp = container.find("input[type=file]");
        var loading = this.loading = container.find(".loading");
        this.submitBtn = container.find(".ie8formsubmitBtn");
        if (this.statics.ltie9) { //ie8及以下做fix
            label.on("click", function (e) {
                fileInp.click();
            })
            container.hover(function () {
                label.css({ "backgroundColor": "#000000", opacity: 0.8 })
            }, function () {
                label.css({ "backgroundColor": "transparent", opacity: 0 })
            })
            that.fileInp[0].onchange = function (e) {
                that.onFileInpChange()
            }
        }

        
    },
    onFileInpChange: function () {
        var that=this;
        if(!that.fileInp.val()) return false;
        that.onload();
        that.form.submit();

    },
    oncomplete: function (res) {
        var that=this;
        if (res.status == "ok" && res.key) {
            that.onsuccess(res);
        } else if(res.status==="fail"){
            that.onfail(res);
        }
        that.loading.hide();
    },
    onload: function () {
        var that=this;
        that.loading.show();
    },
    onsuccess: function (res) {
        var that=this;
        that.container.css("backgroundImage", "url(" + res.key + ")").children(".imgurl").val(res.key);
        ZFT.Util.STip("success", '上传图片成功!');
    },
    onfail: function (res) {
       return ZFT.Util.STip("fail", res.key);
    },
    

})


    module.exports=ImgUploador;








